package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.dto.VenueReportDrawingCategoryDTO;
import com.eastfair.home.dto.VenueReportDrawingDTO;
import com.eastfair.home.service.VenueManageVenueDrawingCategoryAuditService;
import com.eastfair.venuebooking.api.VenueDrawingCategoryAuditFeign;
import com.eastfair.venueservice.consts.CommonConstants;
import com.eastfair.venueservice.dto.VenueDrawingAuditDTO;
import com.eastfair.venueservice.dto.VenueDrawingCategoryAuditDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 场馆 报馆图纸类型审核服务.
 *
 * @author dqq
 * @date 2022 /11/4
 */
@Service
@Slf4j
public class VenueManageVenueDrawingCategoryAuditServiceImpl implements VenueManageVenueDrawingCategoryAuditService {

    @Resource
    private VenueDrawingCategoryAuditFeign venueDrawingCategoryAuditFeign;

    @Override
    public boolean submit(VenueDrawingCategoryAuditDTO param) {
        log.info("submit - 提交报馆图纸审核记录, param={}", param);
        R<Boolean> result = venueDrawingCategoryAuditFeign.submit(param);
        log.info("submit - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean submitByVenueReportDrawingCategory(VenueReportDrawingCategoryDTO param) {
        CopyOptions copyOptions = CopyOptions
                .create()
                .setIgnoreProperties(CommonConstants.COMMON_IGNORE_PROPERTIES);
        // VenueReportDrawingCategoryDTO 转换为 VenueDrawingCategoryAuditDTO
        VenueDrawingCategoryAuditDTO venueDrawingCategoryAuditDTO = BeanUtil.toBean(param, VenueDrawingCategoryAuditDTO.class);
        // VenueReportDrawingDTO列表 转换为 VenueDrawingAuditDTO列表
        List<VenueReportDrawingDTO> venueReportDrawingList = param.getVenueReportDrawingList();
        List<VenueDrawingAuditDTO> venueDrawingAuditList = venueReportDrawingList
                .stream()
                .map(venueReportDrawingDTO -> BeanUtil.toBean(venueReportDrawingDTO, VenueDrawingAuditDTO.class, copyOptions))
                .collect(Collectors.toList());
        venueDrawingCategoryAuditDTO.setVenueDrawingAuditList(venueDrawingAuditList);
        return submit(venueDrawingCategoryAuditDTO);
    }
}